﻿using System;
using System.Windows.Forms;
using HardwareHelperLib;

namespace SwitchNet
{
    public delegate void StatusUpdate( string message );
    
    class NICProfileManager
	{
		#region Variables

        private HH_Lib hwh = new HH_Lib();

		private NICProfile _NICProfile;

		public event StatusUpdate OnStatusUpdate;

		#endregion

		#region Constructors
		
		public NICProfileManager( NICProfile nicProfile )
		{
            _NICProfile = nicProfile;
		}

		#endregion

		#region Public Methods

        public void Run()
        {
            applyNIC(_NICProfile);
        }

		#endregion

		#region Private methods

		/// <summary>
		/// Applies configuration for the specified NIC
		/// </summary>
		/// <param name="nicName"></param>
        private void applyNIC( NICProfile nicProfile )
		{
            string nicName = nicProfile.Name;

			if( null == nicProfile )
                return;

			//UpdateStatus( "Setting configuration for: " + nicName );

            try
            {
                if (nicProfile.UseDHCP)
                {
                    UpdateStatus("UseDHCP.");
                    NetworkManagement.SetDHCP(nicName);

                    UpdateStatus("Set DHCP OK!");
                }
                else
                {
                    UpdateStatus("Not use DHCP.");
                    NetworkManagement.SetIPAddress(nicProfile);

                    UpdateStatus("Set IP OK!");
                }

                if (nicProfile.SetMAC)
                {
                    UpdateStatus("");
                    NetworkManagement.SetMACAddress(nicProfile);
                    UpdateStatus("Set MAC Address: [" + nicProfile.MAC + "] Done");
                    UpdateStatus("");

                    if (MessageBox.Show("Change MAC Address need Re-Enable NIC, Re-Enable now?", "Need Re-Enable", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                    {
                        reEnableNIC(nicProfile);
                    }
                    else
                    {
                        notReEnableNIC(nicProfile);
                    }
                }
            }
            catch (Exception x)
            {
                UpdateStatus("Error occured while setting network configuration.");
                UpdateStatus(x.Message);
                return;
            }
            finally
            {

                UpdateStatus("");
                UpdateStatus("Click [OK] to continue ....");
            }
		}

        private void reEnableNIC(NICProfile nicProfile)
        {
            int nicNameLength = nicProfile.Name.Length;
            int firstSpace = nicProfile.Name.IndexOf(' ');

            string[] DriverDesc = new string[] { nicProfile.Name.Substring(firstSpace + 1) };

            string DriverName = null;
            foreach (string str in DriverDesc)
            {
                DriverName += str;
            }

            UpdateStatus("Disabling [" + DriverName + "] ... ...");
            if (hwh.SetDeviceState(DriverDesc, false))
            {
                UpdateStatus("[" + DriverName + "] Disabled!");

                UpdateStatus("Enabling [" + DriverName + "] ... ...");
                if (hwh.SetDeviceState(DriverDesc, true))
                {
                    UpdateStatus("[" + DriverName + "] Enabled!");
                }
                else
                {
                    UpdateStatus("Could not enable [" + DriverName + "] !");
                }

            }
            else
            {
                UpdateStatus("Could not disable [" + DriverName + "] !");
            }
        }

        private void notReEnableNIC(NICProfile nicProfile)
        {
            UpdateStatus("The new MAC address will not using before restart computer!");
        }

		/// <summary>
		/// Raises event to update status
		/// </summary>
		/// <param name="message"></param>
		private void UpdateStatus( string message )
		{
			if( null != OnStatusUpdate )
				OnStatusUpdate( message + Environment.NewLine );
		}
		
		#endregion
	}
}
